package cn.nfwx.tv.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import cn.nfwx.tv.R;

/**
 * Created by zhouzechao on 2017/11/16.
 */
public class BatteryView extends View {

    private final String TAG = BatteryView.class.getSimpleName();

    private int width;
    private int height;

    private int mPower = 100;
    private int mColor = Color.WHITE;

    private int pagging = 3;

    private int raduis = 4;
    private int bolt = R.mipmap.bolt;
    private boolean mBolt = false;

    public BatteryView(Context context) {
        this(context, null);
    }

    public BatteryView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //对View上的內容进行测量后得到的View內容占据的宽度
        width = getMeasuredWidth();
        //对View上的內容进行测量后得到的View內容占据的高度
        height = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(mColor);
        paint.setStyle(Paint.Style.STROKE);
        float strokeWidth = (width / 20.f) + 0.2f;
        float strokeWidth_2 = strokeWidth / 3;
        float rectWidth = 1;
        paint.setStrokeWidth(strokeWidth);
        RectF r1 = new RectF(strokeWidth_2, strokeWidth_2, width - strokeWidth - strokeWidth_2 - rectWidth, height - strokeWidth_2);
        //设置外边框颜色为黑色
        paint.setColor(Color.WHITE);
        canvas.drawRoundRect(r1, raduis, raduis, paint);
        paint.setStrokeWidth(0);
        paint.setStyle(Paint.Style.FILL);
        //画电池内矩形电量
        if (!mBolt) { // 是否再充电
            float offset = (width - strokeWidth * 2) * mPower / 100.f;
            RectF r2 = new RectF(strokeWidth + pagging, strokeWidth + pagging, offset - pagging - rectWidth, height - strokeWidth - pagging);
            //根据电池电量决定电池内矩形电量颜色
            if (mPower < 30) {
                paint.setColor(Color.RED);
            }
            if (mPower >= 30 && mPower < 50) {
                paint.setColor(Color.WHITE);
            }
            if (mPower >= 50) {
                paint.setColor(Color.WHITE);
            }
            canvas.drawRect(r2, paint);
        } else {
            Bitmap b = BitmapFactory.decodeResource(getResources(), bolt);
            int left = width / 2 - b.getWidth() / 2;
            int top = height / 2 - b.getHeight() / 2;
            canvas.drawBitmap(b, left, top, paint);
            b.recycle();
        }
        //画电池头
        RectF r3 = new RectF(width - strokeWidth - rectWidth, height * 0.3f, width, height * 0.75f);
        //设置电池头颜色为黑色
        paint.setColor(Color.WHITE);
        canvas.drawRect(r3, paint);
    }

    /**
     * 设置电池电量
     *
     * @param power
     */
    public void setPower(int power) {
        this.mPower = power;
        if (mPower < 0) {
            mPower = 100;
        }
        invalidate();//刷新VIEW
    }

    /**
     * 充电状态设置
     *
     * @param bolt
     */
    public void setBolt(boolean bolt) {
        mBolt = bolt;
    }

    /**
     * 设置电池颜色
     *
     * @param color
     */
    public void setColor(int color) {
        this.mColor = color;
        invalidate();
    }

    /**
     * 获取电池电量
     *
     * @return
     */
    public int getPower() {
        return mPower;
    }
}
